require "BagTableLayer"
require "HeroEquipLayer"

BSHCLayer = {}
BSHCLayerFunc = {}
BSHCLayerFunc.__index = BSHCLayerFunc
setmetatable(BSHCLayer, BSHCLayerFunc)
--BSHCLayer.NodeID = {TableBK=1003, QiangHuaButton=1004, Item1=2001, Item2=2002}

BSHCLayerFunc.getNode = function()
  local l = {}
  setmetatable(l, BSHCLayerFunc)
  l.node = CCBReader:load("ZhuangBeiLayer2.ccbi")
  BSHCLayer.instance = l
  l:init()
  return l
end
BSHCLayerFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(BSHCLayer:getNode().node)
  print("BSHCLayer.getScene", sc)
  return sc
end

BSHCLayerFunc.init = function(self)
  --local button = self.node:getChildByTag()
  --setObjectExtTable(button, self);
  local tb = self.node:getChildByTag(1003)
  self.bagTable = BagTable:create(TypeCast(tb, "CCSprite"), self, "onClickBagEquip", 2)
  self.node:addChild(self.bagTable.node)

  self.hechengButton = TypeCast(self.node:getChildByTag(1020), "CCButton")
  setObjectExtTable(self.hechengButton, self)
  self.hechengButton:setEnabled(false);
end
BSHCLayerFunc.onClickBagEquip = function(self, node, event)
  print(self, node, node:getTag())
  if node:getTag()>=100000 then
    self:setEquipInfo(node:getTag()-100000)
  end
  --self:setEquipInfo(node:getTag())
end
BSHCLayerFunc.clearEquipInfo = function(self)
  self.node:removeChild(self.leftSp, true)
  self.node:removeChild(self.topSp, true)
  self.node:removeChild(self.rightSp, true)
  self.currentItem = nil 
end

BSHCLayerFunc.setEquipInfo = function(self, equipIndex)
  if equipIndex==nil and self.currentItem==nil then
    return
  end
  if equipIndex==nil then
    equipIndex = self.currentItem.index
  end
  local bagitem = Item:createWithIndex(equipIndex)
  --local equip = Equip:create(equipIndex)
  if bagitem.itemConfig.alias == "steadyDrug" then
    local sp  = addSpriteFromBK(self.node, 2002, nameFromPath(bagitem.itemConfig.asset))
    self.rightSp = sp
    self.currentDrug = 1
  end
  if (bagitem.itemConfig.alias=="qualityStone" or bagitem.itemConfig.alias=="levelStone") 
      and bagitem.itemLevel.nextStoneId~=0  then
    self:clearEquipInfo() 
    local tmpItemLevel = UserData.config.models.Stone[bagitem.itemLevel.nextStoneId]
    local tmpItem = Item:createWithId(tmpItemLevel.itemId)
    
    local itemLayer = bagitem:createItemLayer("onClickBagEquip", self, tmpItemLevel.costStoneNum)
    itemLayer:setPosition(self.node:getChildByTag(2001):getPosition())
    self.node:addChild(itemLayer)
    self.leftSp = itemLayer
    if bagitem.itemInfo.n >= tmpItemLevel.costStoneNum then
      TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(true);
    else
      TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(false);
    end
    itemLayer = tmpItem:createItemLayer("onClickBagEquip", self, 1, false)
    itemLayer:setPosition(self.node:getChildByTag(2003):getPosition())
    self.node:addChild(itemLayer)
    self.topSp = itemLayer
    TypeCast(self.node:getChildByTag(1010), "CCLabelTTF"):setString(string.format("%d%%", bagitem.itemLevel.upRate*100))
    self.currentItem = bagitem--equipIndex 
  else
    showMessage("不能合成", ccc3(255,255,0))
  end
end

BSHCLayerFunc.showShineAction = function(self, bksp)
  CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile("shine.plist")
  local sp = CCSprite:createWithSpriteFrameName("shine0001.png")
  sp:setPosition(bksp:getPosition())
  self.node:addChild(sp)
  local ac = ActionUtil.createAction("shine.plist",0.02,"")
  sp:runAction(ActionUtil.createSeqAction({ac, CCCallFuncN:create(onShineEnd)}))
end
function onShineEnd(node)
  setTouchEnabledAll(ZhuangBeiScene.instance.node, true)
  BSHCLayer.instance.hechengButton:setEnabled(false);
  BSHCLayer.instance:setEquipInfo()
  --BSHCLayer.instance:clearEquipInfo()
  node:getParent():removeChild(node, true)
  
  if BSHCLayer.instance.bagTable~=nil then
    BSHCLayer.instance.node:removeChild(BSHCLayer.instance.bagTable.node, true)
  end 
  local tb = BSHCLayer.instance.node:getChildByTag(1003)
  BSHCLayer.instance.bagTable = BagTable:create(TypeCast(tb, "CCSprite"), BSHCLayer.instance, "onClickBagEquip", 2)
  BSHCLayer.instance.node:addChild(BSHCLayer.instance.bagTable.node)
end
BSHCLayerFunc.onHeChengButton = function(self, node, event)
  print("BSHCLayerFunc.onHeChengButton")
  NetworkRequest.hechengItem(self.currentItem.itemLevel.id, 0, "onHeChengComplete", self)
  LoadScene:showLoadingScene()
  --setTouchEnabledAll(ZhuangBeiScene.instance.node, false)
  --self.hechengButton:setEnabled(false);
  --self.currentItemIndex
  --self:showShineAction(self.topSp)
end

BSHCLayerFunc.onHeChengComplete = function(self, data)
  print("BSHCLayerFunc.onHeChengButton", data)
  --self.currentItem = nil
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self:showShineAction(self.topSp)
  else
    showMessage(res.msg, ccc3(255,255,0))
    --setTouchEnabledAll(ZhuangBeiScene.instance.node, true)
    BSHCLayer.instance.hechengButton:setEnabled(false);
  end
end


